package com.zyd.util;

import java.util.List;

/**
 * @Author: caicaicainiaoa
 * @Date: 2019/4/1  19:41
 */
public class PointUtils {
    /**
     * 坐标内部类
     *
     * @Author: caicaicainiaoa
     * @Date: 2019/4/1  19:41
     */
    public static class Point {

        private Double longitude;
        private Double latitude;

        public Point(Double longitude, Double latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }

        public Point() {
            this.longitude = 0d;
            this.latitude = 0d;
        }

        public Double getLongitude() {
            return longitude;
        }

        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }

        public Double getLatitude() {
            return latitude;
        }

        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }
    }

    /**
     * 判断该坐标是否在坐标数组所话的多边形内
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @param ps
     * @return
     */
    public static boolean isPtInPoly(double longitude, double latitude, Point[] ps) {
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (ps.length < 3) {
            return false;
        }
        iSum = 0;
        iCount = ps.length;
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = ps[iIndex].getLongitude();
                dLat1 = ps[iIndex].getLatitude();
                dLon2 = ps[0].getLongitude();
                dLat2 = ps[0].getLatitude();
            } else {
                dLon1 = ps[iIndex].getLongitude();
                dLat1 = ps[iIndex].getLatitude();
                dLon2 = ps[iIndex + 1].getLongitude();
                dLat2 = ps[iIndex + 1].getLatitude();
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((latitude >= dLat1) && (latitude < dLat2)) || ((latitude >= dLat2) && (latitude < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - latitude)) / (dLat1 - dLat2);
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：
                    if (dLon < longitude) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断该坐标是否在坐标数组所话的多边形内
     *
     * @param point 坐标
     * @param ps    坐标组
     * @return
     */
    public static boolean isPtInPoly(Point point, Point[] ps) {
        return isPtInPoly(point.getLongitude(), point.getLatitude(), ps);
    }
    public static boolean isPtInPoly(Point point, List<Point> ps) {
        return isPtInPoly(point.getLongitude(), point.getLatitude(), ps.toArray(new Point[]{}));
    }
    public static boolean isPtInPoly(double longitude, double latitude, List<Point> ps) {
        return isPtInPoly(longitude, latitude, ps.toArray(new Point[]{}));
    }


}
